﻿using System;
using System.Resources;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;

namespace IceTea.Resources
{
    /// <summary>
    /// Helper class for getting valid CultureInfo
    /// </summary>
    public class CultureHelper
    {
        private CultureHelper() { }

        private static IList<string> _cultures = new List<string> {
            "en-US", //English (United States) 
            "zh-CN"  //Chinese (PRC)
        };

        public static CultureInfo GetDefaultCulture()
        {
            return CultureInfo.CreateSpecificCulture(_cultures[0]);
        }

        public static CultureInfo GetValidCulture(string localeName)
        {
            string culturename = null;

            if (!string.IsNullOrEmpty(localeName))
            {
                if (_cultures.Contains(localeName))
                    culturename = localeName;
                else 
                    culturename = (from c in _cultures
                                   where c.StartsWith(localeName.Substring(0, 2))
                                   select c).FirstOrDefault();
            }
                
            if (!string.IsNullOrEmpty(culturename))
                return CultureInfo.CreateSpecificCulture(culturename);
            
            return GetDefaultCulture();
        }
    }
}
